from mitmproxy.http import flow
#mitmdump -q -s  mitm.py -p 8080
import json
import re
def request(flow:flow):
    # 获取所有头信息，包含Host、User-Agent、Content-type等字段
    # print(flow.request.headers)

    # 域名
    # print(flow.request.host)

    # 请求方式：POST、GET等
    # print(flow.request.method)

    # 请求类型：http、https
    # print(flow.request.scheme)

    # 请求的路径，URL除域名之外的内容
    # print(flow.request.path)

    # 请求中body的内容，有一些http会把请求参数放在body里面，可通过此方法获取，返回字典类型
    # print(flow.request.get_text())

    # 返回MultiDictView类型的数据，URL的键值参数
    # print(flow.request.query)


    # 完整的请求地址，包含域名及请求参数，但是不包含放在body里面的请求参数
    if 'https://www.baidu.com' in flow.request.url:
        # 取得请求参数wd的值
        # print(flow.request.query.get('wd'))

        # 取得所有请求参数
        print(list(flow.request.query.keys()))

        # 修改请求参数
        # flow.request.query.set_all('wd',['python'])
        # 打印修改过后的参数
        print(flow.request.query.get('wd'))



def response(flow: flow):
    # 状态码
    # print(flow.response.status_code)

    # 返回内容，已解码
    # print(flow.response.text)

    # 返回内容，Bytes类型
    # print(flow.response.content)

    # 取得响应的文本
    # print(flow.response.get_text())

    # 修改响应的文本
    # flow.response.set_text('123')

    # 返回404
    # flow.response = flow.response.make(404)

    # 修改淘宝对selenium的js检测文件
    targetUrl = 'https://g.alicdn.com/AWSC/uab/122.js'
    if  targetUrl in flow.request.url:
        taobao_js =flow.response.get_text()
        taobao_js = taobao_js.replace('!function(){function','!function (){Object.defineProperties(navigator,{webdriver: {get: () => false}})function')
        flow.response.set_text(taobao_js)
        print('已修改')

    # 淘宝搜索商品时，自动打印商品信息
    if 'https://s.taobao.com/search' in flow.request.url:
        start = flow.response.text.strip().index('{')
        end = -2
        print(json.loads(flow.response.text.strip()[start: end])['mods']['itemlist']['data']['auctions'])


    # baseurl = 'http://qwx-test.tangees.com/api/role/permission?isHiddenErrorMessage=true'
    # filter_list =['js','css','png','svg','ico']
    # if baseurl in flow.request.url:
    #     if flow.request.url.split('.')[-1] not in filter_list:
    #         with open('permissions.json', 'w',encoding='utf-8') as pf:
    #             json.dump(flow.response.text, pf,ensure_ascii=False)

    # detailgroups = 'wecom/official-account/list'
    # filter_list =['js','css','png','svg','ico']
    # if detailgroups in flow.request.url:
    #     if flow.request.url.split('.')[-1] not in filter_list:
    #         data = json.loads(flow.response.get_text())
    #         data['data']['total'] = 30
    #         pagesize = data['data']['size']
    #         start = data['data']['current']*pagesize
    #         try:
    #             if len(data['data']['records'])>0:
    #                 records = data['data']['records'] * pagesize
    #                 data['data']['records'] = records[start:start+pagesize]
    #             else:
    #                 records = [{"momentId": "1410070612047028227", "interactTime": 1625040058000, "wxStaffId": "ZhuJianHao", "staffName": "\u6731\u5065\u8c6a", "staffAvatar": "http://wework.qpic.cn/bizmail/q8ZxCnzq6J8uQaQaZX8VazSXfp186A2pWgNGdicjxJszJBicribrBfiafg/0", "customerName": "zhujh_tx", "wxCustomerId": "wmTxqkEAAAXXeyziXq4_1BBBb95rl6OQ", "customerAvatar": "http://wx.qlogo.cn/mmhead/k6DgARWgMbiaBlBaicfu6QEnKpAcsVuULnjMAs8GFM0vk/0", "interactType": 0, "interactTypeStr": "\u5ba2\u6237\u70b9\u8d5e"}, {"momentId": "1410070612047028227", "interactTime": 1625040044000, "wxStaffId": "ZhuJianHao", "staffName": "\u6731\u5065\u8c6a", "staffAvatar": "http://wework.qpic.cn/bizmail/q8ZxCnzq6J8uQaQaZX8VazSXfp186A2pWgNGdicjxJszJBicribrBfiafg/0", "customerName": "\u6731\u5065\u8c6a", "wxCustomerId": "ZhuJianHao", "customerAvatar": "http://wework.qpic.cn/bizmail/q8ZxCnzq6J8uQaQaZX8VazSXfp186A2pWgNGdicjxJszJBicribrBfiafg/0", "interactType": 1, "interactTypeStr": "\u5458\u5de5\u70b9\u8d5e"}, {"momentId": "1410070612047028227", "interactTime": 1625038042000, "wxStaffId": "60da9764071bac462b4169d1", "staffName": "OPPO\u4f01\u5fae1", "staffAvatar": "https://wework.qpic.cn/bizmail/N6T1qTnf2FlG2h7lKvNRDwArsOICKxnxxmQhjrOw72AYCbU6pxcMrw/0", "customerName": "OPPO\u5fae\u4fe1", "wxCustomerId": "wmTxqkEAAA-h7eq0b_6YNsgb5ZTPJx3g", "customerAvatar": "http://wx.qlogo.cn/mmhead/1CHHx9Yq4nGab1uCJ1pyuUCNtPtwnkby0cZhvsLicqcEicjsKtuWBWLg/0", "interactType": 0, "interactTypeStr": "\u5ba2\u6237\u70b9\u8d5e"}, {"momentId": "1410070612047028227", "interactTime": 1625037035000, "wxStaffId": "ZhuJianHao", "staffName": "\u6731\u5065\u8c6a", "staffAvatar": "http://wework.qpic.cn/bizmail/q8ZxCnzq6J8uQaQaZX8VazSXfp186A2pWgNGdicjxJszJBicribrBfiafg/0", "customerName": "Free Joe", "wxCustomerId": "wmTxqkEAAAd3YOoOat4j_1bD2FmTiWYw", "customerAvatar": "http://wx.qlogo.cn/mmhead/KetjXWSVpptGUhVPVw3FwTATGW8yECoGcm0UOibNcjaGKYMN2VNcIxA/0", "interactType": 0, "interactTypeStr": "\u5ba2\u6237\u70b9\u8d5e"}, {"momentId": "1410070612047028227", "interactTime": 1625036997000, "wxStaffId": "ZhuJianHao", "staffName": "\u6731\u5065\u8c6a", "staffAvatar": "http://wework.qpic.cn/bizmail/q8ZxCnzq6J8uQaQaZX8VazSXfp186A2pWgNGdicjxJszJBicribrBfiafg/0", "customerName": "liwuen", "wxCustomerId": "wmTxqkEAAAjlHV9hPwTEC-pkyBs3pNDw", "customerAvatar": "http://wx.qlogo.cn/mmhead/Tjnia6K0WAwzsdbWT0H1ricmkgtzXBXUOcyyc3IYVYm0HcA0JicDPvfpA/0", "interactType": 0, "interactTypeStr": "\u5ba2\u6237\u70b9\u8d5e"}, {"momentId": "1410070612047028227", "interactTime": 1625040058000, "wxStaffId": "ZhuJianHao", "staffName": "\u6731\u5065\u8c6a", "staffAvatar": "http://wework.qpic.cn/bizmail/q8ZxCnzq6J8uQaQaZX8VazSXfp186A2pWgNGdicjxJszJBicribrBfiafg/0", "customerName": "zhujh_tx", "wxCustomerId": "wmTxqkEAAAXXeyziXq4_1BBBb95rl6OQ", "customerAvatar": "http://wx.qlogo.cn/mmhead/k6DgARWgMbiaBlBaicfu6QEnKpAcsVuULnjMAs8GFM0vk/0", "interactType": 0, "interactTypeStr": "\u5ba2\u6237\u70b9\u8d5e"}, {"momentId": "1410070612047028227", "interactTime": 1625040044000, "wxStaffId": "ZhuJianHao", "staffName": "\u6731\u5065\u8c6a", "staffAvatar": "http://wework.qpic.cn/bizmail/q8ZxCnzq6J8uQaQaZX8VazSXfp186A2pWgNGdicjxJszJBicribrBfiafg/0", "customerName": "\u6731\u5065\u8c6a", "wxCustomerId": "ZhuJianHao", "customerAvatar": "http://wework.qpic.cn/bizmail/q8ZxCnzq6J8uQaQaZX8VazSXfp186A2pWgNGdicjxJszJBicribrBfiafg/0", "interactType": 1, "interactTypeStr": "\u5458\u5de5\u70b9\u8d5e"}, {"momentId": "1410070612047028227", "interactTime": 1625038042000, "wxStaffId": "60da9764071bac462b4169d1", "staffName": "OPPO\u4f01\u5fae1", "staffAvatar": "https://wework.qpic.cn/bizmail/N6T1qTnf2FlG2h7lKvNRDwArsOICKxnxxmQhjrOw72AYCbU6pxcMrw/0", "customerName": "OPPO\u5fae\u4fe1", "wxCustomerId": "wmTxqkEAAA-h7eq0b_6YNsgb5ZTPJx3g", "customerAvatar": "http://wx.qlogo.cn/mmhead/1CHHx9Yq4nGab1uCJ1pyuUCNtPtwnkby0cZhvsLicqcEicjsKtuWBWLg/0", "interactType": 0, "interactTypeStr": "\u5ba2\u6237\u70b9\u8d5e"}, {"momentId": "1410070612047028227", "interactTime": 1625037035000, "wxStaffId": "ZhuJianHao", "staffName": "\u6731\u5065\u8c6a", "staffAvatar": "http://wework.qpic.cn/bizmail/q8ZxCnzq6J8uQaQaZX8VazSXfp186A2pWgNGdicjxJszJBicribrBfiafg/0", "customerName": "Free Joe", "wxCustomerId": "wmTxqkEAAAd3YOoOat4j_1bD2FmTiWYw", "customerAvatar": "http://wx.qlogo.cn/mmhead/KetjXWSVpptGUhVPVw3FwTATGW8yECoGcm0UOibNcjaGKYMN2VNcIxA/0", "interactType": 0, "interactTypeStr": "\u5ba2\u6237\u70b9\u8d5e"}, {"momentId": "1410070612047028227", "interactTime": 1625036997000, "wxStaffId": "ZhuJianHao", "staffName": "\u6731\u5065\u8c6a", "staffAvatar": "http://wework.qpic.cn/bizmail/q8ZxCnzq6J8uQaQaZX8VazSXfp186A2pWgNGdicjxJszJBicribrBfiafg/0", "customerName": "liwuen", "wxCustomerId": "wmTxqkEAAAjlHV9hPwTEC-pkyBs3pNDw", "customerAvatar": "http://wx.qlogo.cn/mmhead/Tjnia6K0WAwzsdbWT0H1ricmkgtzXBXUOcyyc3IYVYm0HcA0JicDPvfpA/0", "interactType": 0, "interactTypeStr": "\u5ba2\u6237\u70b9\u8d5e"}]* pagesize
    #                 data['data']['records'] = records[0:pagesize]
    #
    #         except Exception as e:
    #             print(e)
    #         newdata = json.dumps(data)
    #         flow.response.set_text(newdata)

    detailgroups = 'wecom/official-account/list'
    filter_list =['js','css','png','svg','ico']
    if detailgroups in flow.request.url:
        if flow.request.url.split('.')[-1] not in filter_list:
            data = json.loads(flow.response.get_text())
            data['total'] = 30
            pagesize = data['data']['size']
            start = data['data']['current']*pagesize
            try:
                if len(data['records'])>0:
                    records = data['data']['records'] * pagesize
                    data['records'] = records[start:start+pagesize]

            except Exception as e:
                print(e)
            newdata = json.dumps(data)
            flow.response.set_text(newdata)

if __name__ == "__main__":
    import os

    os.system("mitmdump -q -s  mitm.py -p 8889")
